package ACWing.DataStructure.链表;
//828. 模拟栈

import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2022/12/28 15:20
 */
public class AnalogStack {
    static int[]stk=new int[100010];
    static int tt;

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        sc.nextLine();
        while (m-->0){
            String s=sc.next();
            if("push".equals(s)){
                int x=sc.nextInt();
                push(x);
            }
            if("pop".equals(s)){
                if(!empty()){
                    pop();
                }
            }
            if("empty".equals(s)){
                if(empty()){
                    System.out.println("YES");
                }else {
                    System.out.println("NO");
                }
            }
            if("query".equals(s)){
                if(!empty()){
                    System.out.println(query());
                }
            }
        }
    }
    public static void push(int x){
        stk[++tt]=x;
    }
    public static void pop(){
        tt--;
    }
    public static boolean empty(){
        if(tt>0){
            return false;
        }else {
            return true;
        }
    }
    public static int query(){
        return stk[tt];
    }
}
